package com.fang.networkdisk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fang.common.common.CommonPage;
import com.fang.common.entity.bo.NetDisLogBo;
import com.fang.common.util.NDBeanUtils;
import com.fang.networkdisk.entity.po.TbNetDisLog;
import com.fang.networkdisk.entity.vo.LogVo;
import com.fang.networkdisk.mapper.TbNetDisLogMapper;
import com.fang.networkdisk.service.TbNetDisLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Bernie_fang
 * @since 2025-04-30
 */
@Service
public class TbNetDisLogServiceImpl extends ServiceImpl<TbNetDisLogMapper, TbNetDisLog> implements TbNetDisLogService {

    @Resource
    private TbNetDisLogMapper tbNetDisLogMapper;

    @Override
    public int addLog(NetDisLogBo disLogBo) {
        TbNetDisLog disLog = new TbNetDisLog();
        NDBeanUtils.copyListProperties(disLogBo, disLog);
        String requestBody = disLogBo.getRequestBody();
        String headers = disLogBo.getHeaders();
        String responseData = disLogBo.getResponseData();
        disLog.setRequestBody(requestBody.substring(0, Math.min(requestBody.length(), 250)));
        disLog.setResponseData(responseData.substring(0, Math.min(requestBody.length(), 255)));
        disLog.setHeaders(headers.substring(0, Math.min(requestBody.length(), 250)));
        return tbNetDisLogMapper.insertDisLog(disLog);
    }

//    @Override
//    public int updateLog(NetDisLogBo disLogBo) {
//        TbNetDisLog disLog = new TbNetDisLog();
//        NDBeanUtils.copyListProperties(disLogBo, disLog);
//        return tbNetDisLogMapper.updateDisLogById(disLog);
//    }

    @Override
    public CommonPage queryLogRecord(NetDisLogBo disLogBo) {
        disLogBo.setOffset((disLogBo.getCurPage() - 1) * disLogBo.getPageSize());
        List<LogVo> logVos = this.tbNetDisLogMapper.queryLog(disLogBo);
        int total = this.tbNetDisLogMapper.queryLogCount(disLogBo);
        CommonPage result = new CommonPage(disLogBo.getCurPage(), total, logVos);
        return result;
    }

}
